/**
Copyright (c) 2008 Arnaud Fabre <arnaud333@gmail.com>

This file is part of Vos Tutos Offline.

Vos Tutos Offline is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Vos Tutos Offline is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Vos Tutos Offline.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "zcode.h"
#include "mdown.h"
#include "global.h"

#include <QFile>
#include <QVector>
#include <QTextStream>

QDomElement Zcode::partToDom(Part* p, QDomDocument& doc)
{
    QDomElement part = doc.createElement("souspartie");
    QDomElement deTitle = doc.createElement("titre");
    QDomElement deContents = doc.createElement("texte");
    deTitle.appendChild(doc.createTextNode("\n"));
    deTitle.appendChild(doc.createCDATASection(  p->getTitle()  ));
    deTitle.appendChild(doc.createTextNode("\n"));
    deContents.appendChild(doc.createTextNode("\n"));
    deContents.appendChild(doc.createCDATASection( Mdown::toZcode(p->getContents()) ));
    deContents.appendChild(doc.createTextNode("\n"));
    part.appendChild(deTitle);
    part.appendChild(deContents);

    return part;
}

QDomElement Zcode::miniToDom(MiniTutorial* mt, QDomDocument& doc, bool toBt)
{

    // Pour les licences 
    QVector<int> eqc;
    eqc << 1 << 2 << 7 << 5 << 6 << 3 << 4;


    QDomElement minituto;
    if(toBt)
	minituto = doc.createElement("chapitre");
    else
	minituto = doc.createElement("minituto");
    
    if(!toBt) minituto.setAttribute("generator", VTO::version());
    QDomElement deTitle = doc.createElement("titre");
    QDomElement deProgress = doc.createElement("avancement");
    QDomElement deDifficult = doc.createElement("difficulte");
    QDomElement deIntro = doc.createElement("introduction");
    QDomElement deParts = doc.createElement("sousparties");
    QDomElement deConclu = doc.createElement("conclusion");

    deTitle.appendChild(doc.createTextNode("\n"));
    deTitle.appendChild(doc.createCDATASection(mt->getTitle()));
    deTitle.appendChild(doc.createTextNode("\n"));

    deDifficult.appendChild(doc.createTextNode("\n"));
    deDifficult.appendChild(doc.createCDATASection(
            QString::number(mt->getDifficult())));
    deDifficult.appendChild(doc.createTextNode("\n"));

    deProgress.appendChild(doc.createTextNode("\n"));
    deProgress.appendChild(doc.createCDATASection(
            QString::number(mt->getProgress())));
    deProgress.appendChild(doc.createTextNode("\n"));

    deIntro.appendChild(doc.createTextNode("\n"));
    deIntro.appendChild(doc.createCDATASection( Mdown::toZcode(  mt->getIntro()->getContents() ) ));
    deIntro.appendChild(doc.createTextNode("\n"));

    deConclu.appendChild(doc.createTextNode("\n"));
    deConclu.appendChild(doc.createCDATASection( Mdown::toZcode(  mt->getConclu()->getContents() ) ));
    deConclu.appendChild(doc.createTextNode("\n"));
    
    for(QList<Part*>::const_iterator it = mt->allParts().begin(); it != mt->allParts().end(); ++it)
	deParts.appendChild( partToDom(*it, doc) );
    
    minituto.appendChild(deTitle);
    minituto.appendChild(deProgress);
    if(!toBt)
    {
        QDomElement deLicence = doc.createElement("licence");
        deLicence.appendChild(doc.createTextNode("\n"));
        deLicence.appendChild(doc.createCDATASection(
            QString::number(eqc[mt->getLicence()])));
        deLicence.appendChild(doc.createTextNode("\n"));
        minituto.appendChild(deLicence);
    }
    minituto.appendChild(deDifficult);
    minituto.appendChild(deIntro);
    minituto.appendChild(deParts);
    minituto.appendChild(deConclu);

    return minituto;
}

QDomElement Zcode::bigToDom(BigTutorial* bt, QDomDocument& doc)
{
    // Pour les licences
    QVector<int> eqc;
        eqc << 1 << 2 << 7 << 5 << 6 << 3 << 4;

    QDomElement bigtuto = doc.createElement("bigtuto");
    QDomElement deTitle = doc.createElement("titre");
    QDomElement deProgress = doc.createElement("avancement");
    QDomElement deLicence = doc.createElement("licence");
    QDomElement deDifficult = doc.createElement("difficulte");
    QDomElement deIntro = doc.createElement("introduction");
    QDomElement deChapters = doc.createElement("parties");
    QDomElement deConclu = doc.createElement("conclusion");
    
    bigtuto.setAttribute("generator", VTO::version());

    deTitle.appendChild(doc.createTextNode("\n"));
    deTitle.appendChild(doc.createCDATASection( bt->getTitle() ));
    deTitle.appendChild(doc.createTextNode("\n"));

    deLicence.appendChild(doc.createTextNode("\n"));
    deLicence.appendChild(doc.createCDATASection(
        QString::number(eqc[bt->getLicence()])));
    deLicence.appendChild(doc.createTextNode("\n"));

    deDifficult.appendChild(doc.createTextNode("\n"));
    deDifficult.appendChild(doc.createCDATASection(
        QString::number(bt->getDifficult())));
    deDifficult.appendChild(doc.createTextNode("\n"));

    deProgress.appendChild(doc.createTextNode("\n"));
    deProgress.appendChild(doc.createCDATASection(
        QString::number(bt->getDifficult())));
    deProgress.appendChild(doc.createTextNode("\n"));

    deIntro.appendChild(doc.createTextNode("\n"));
    deIntro.appendChild(doc.createCDATASection( Mdown::toZcode(  bt->getIntro()->getContents()  ) ));
    deIntro.appendChild(doc.createTextNode("\n"));

    deConclu.appendChild(doc.createTextNode("\n"));
    deConclu.appendChild(doc.createCDATASection( Mdown::toZcode(  bt->getConclu()->getContents()  )));
    deConclu.appendChild(doc.createTextNode("\n"));

    for(QList<BigTutorialPart*>::const_iterator it = bt->allParts().begin(); it != bt->allParts().end(); ++it)
    {	
	QDomElement chapter = doc.createElement("partie");
	QDomElement deTitle = doc.createElement("titre");
	QDomElement deProgress = doc.createElement("avancement");
	QDomElement deDifficult = doc.createElement("difficulte");
    	QDomElement deIntro = doc.createElement("introduction");
    	QDomElement deMinitutos = doc.createElement("chapitres");
	QDomElement deConclu = doc.createElement("conclusion");

        deDifficult.appendChild(doc.createTextNode("\n"));
        deDifficult.appendChild(doc.createCDATASection(
                QString::number((*it)->getDifficult())));
        deDifficult.appendChild(doc.createTextNode("\n"));

        deProgress.appendChild(doc.createTextNode("\n"));
        deProgress.appendChild(doc.createCDATASection(
                QString::number((*it)->getProgress())));
        deProgress.appendChild(doc.createTextNode("\n"));

        deTitle.appendChild(doc.createTextNode("\n"));
        deTitle.appendChild(doc.createCDATASection((*it)->getTitle()));
        deTitle.appendChild(doc.createTextNode("\n"));

        deIntro.appendChild(doc.createTextNode("\n"));
        deIntro.appendChild(
	    doc.createCDATASection(Mdown::toZcode((*it)->getIntro()->getContents()  ) ));
        deIntro.appendChild(doc.createTextNode("\n"));

        deConclu.appendChild(doc.createTextNode("\n"));
        deConclu.appendChild(doc.createCDATASection( Mdown::toZcode( (*it)->getConclu()->getContents()  ) ));
        deConclu.appendChild(doc.createTextNode("\n"));

	for(QList<MiniTutorial*>::const_iterator it2 = (*it)->allChapters().begin(); it2 != (*it)->allChapters().end(); ++it2)	
	    deMinitutos.appendChild(miniToDom((*it2), doc, true));

	chapter.appendChild(deTitle);
	chapter.appendChild(deProgress);
	chapter.appendChild(deDifficult);
	chapter.appendChild(deIntro);
	chapter.appendChild(deMinitutos);
	chapter.appendChild(deConclu);
		
	deChapters.appendChild(chapter);
    }

    bigtuto.appendChild(deTitle);
    bigtuto.appendChild(deProgress);
    bigtuto.appendChild(deLicence);
    bigtuto.appendChild(deDifficult);
    bigtuto.appendChild(deIntro);
    bigtuto.appendChild(deChapters);
    bigtuto.appendChild(deConclu);

    return bigtuto;
}

void Zcode::partToFile(Part* part, const QString& fname)
{
    QDomDocument doc("Part");
    QDomElement root = partToDom(part, doc);
    
    doc.appendChild( root );

    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
	QTextStream ts( &file );
	ts << doc.toString();
	file.close();
    }
    else
	throw 401;
}

void Zcode::miniToFile(MiniTutorial* mt, const QString& fname)
{
    QDomDocument doc("Minituto");
    QDomElement root = miniToDom(mt, doc);
    
    doc.appendChild( root );

    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
	QTextStream ts( &file );
        ts << doc.toString();
	file.close();
    }
    else
	throw 401;
}

void Zcode::bigToFile(BigTutorial* bt, const QString& fname)
{
    QDomDocument doc("BigTuto");
    QDomElement root = bigToDom(bt, doc);
    
    doc.appendChild( root );

    QFile file(fname);
    if( file.open(QIODevice::WriteOnly) )
    {
	QTextStream ts( &file );
	ts << doc.toString();
	file.close();
    }
    else
	throw 401;
}
	
